﻿namespace ClassLibrary.DataLayer.DataObjects
{
    using ClassLibrary.BusinessLayer.BusinessObjects;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    public class AgentsDAO : IAgentsDAO
    {
        public bool DeleteAgents(Agents clsAgents)
        {
            return clsAgents.Save("D");
        }

        public IList<Agents> GetAgents(Agents clsAgents)
        {
            DataTable dt = Db.GetDataTable(clsAgents.CreateGetCommandParameters());
            clsAgents.LoadDataToCollection(dt.Rows[0]);
            if (dt.Rows[0] == null)
            {
                throw new Exception("Agents doesn't exist");
            }
            IList<Agents> list = new List<Agents>();
            list.Add(clsAgents);
            return list;
        }

        public IList<Agents> GetAgents(string agent)
        {
            Agents clsAgents = new Agents();
            clsAgents._Agent = agent;
            return this.GetAgents(clsAgents);
        }

        public IList<Agents> GetsAgents()
        {
            Agents clsAgents = new Agents();
            IDataReader dtReader = Db.GetDataReader(clsAgents.CreateGetsCommandParameters());
            IList<Agents> list = new List<Agents>();
            while (dtReader.Read())
            {
                clsAgents = new Agents();
                clsAgents.LoadDataToCollection(dtReader);
                list.Add(clsAgents);
            }
            return list;
        }

        public DataTable GetsAgents(string SPName, DbParameter[] param)
        {
            Agents clsAgents = new Agents();
            return Db.GetDataTable(clsAgents.GetsDataReader(SPName, param));
        }

        public bool InsertAgents(Agents clsAgents)
        {
            return clsAgents.Save("I");
        }

        public int InsertAgentsReturnIdentity(Agents clsAgents)
        {
            return clsAgents.Save("I", true);
        }

        public string InsertAgentsReturnOutput(Agents clsAgents)
        {
            return clsAgents.Save("I", "@AgentsID");
        }

        public IList<Agents> ListAgents(Agents clsAgents)
        {
            IDataReader dtReader = Db.GetDataReader(clsAgents.CreateListCommandParameters());
            IList<Agents> list = new List<Agents>();
            while (dtReader.Read())
            {
                Agents clsAgentss = new Agents();
                clsAgentss.LoadDataToCollection(dtReader);
                list.Add(clsAgentss);
            }
            return list;
        }

        public IList<Agents> ListAgents(string agent)
        {
            Agents clsAgents = new Agents();
            clsAgents._Agent = agent;
            return this.ListAgents(clsAgents);
        }

        public bool UpdateAgents(Agents clsAgents)
        {
            return clsAgents.Save("U");
        }

        public int UpdateAgents(string SPName, DbParameter[] param)
        {
            Agents clsAgents = new Agents();
            return Db.ExecuteNonQuery(clsAgents.UpdateQuery(SPName, param));
        }
    }
}

